package com.tnstart.db
{
	import com.tnstart.vo.Config;
	
	import flash.data.SQLConnection;
	import flash.data.SQLResult;
	import flash.data.SQLStatement;
	import flash.errors.SQLError;

	public class ConfigDAO
	{
		private var _table:String = "config";
		
		private var _conn:SQLConnection;
		
		
		public function ConfigDAO(conn:SQLConnection)
		{
			_conn = conn;
			createTable();
			
			
		}
		
		
		public function createTable():void
		{
			var createStmt:SQLStatement = new SQLStatement();
			createStmt.sqlConnection = _conn;
			var sql:String = "";
			sql += "CREATE TABLE IF NOT EXISTS config (";
			sql += "	id		INTEGER PRIMARY KEY,";
			sql += "	host	TEXT,";
			sql += "	port		INTEGER,";
			sql += "	apptitle		TEXT";
			sql += ")";
			createStmt.text = sql;
			
			try
			{
				createStmt.execute();
			}
			catch (error:SQLError)
			{
				trace("Error creating table");
				trace("CREATE TABLE error:", error);
				trace("error.message:", error.message);
				trace("error.details:", error.details);
				return;
			}
		}
		
		public function get(id:int):Config{
			var query:String = "";
			
			query = "SELECT * " + 
					"FROM  config " + 
					"WHERE 	id = :id";
			
			var sqlSelect:SQLStatement = new SQLStatement();
			sqlSelect.sqlConnection = _conn;
			sqlSelect.parameters[":id"] = id;
			
			sqlSelect.text = query;
			sqlSelect.execute();
			var result:SQLResult =  sqlSelect.getResult();
			
			var config:Config = convertPlainObjectToItem(result.data[0]);
			
			return config;
		}
			
		private function convertPlainObjectToItem(obj:Object):Config
		{
			var config:Config = new Config();
			config.id = obj.id;
			config.host = obj.host;
			config.port = obj.port;
			config.apptitle = obj.apptitle;
			return config;
		}		
		
		public function save(config:Config):void {
			if(config.id != 0){
				update(config);
			}
			else{
				insert(config);
			}
		}
		
		public function insert(config:Config):void {
			var query:String = "INSERT INTO config (" + 
				"host," + 
				"port," + 
				"apptitle)" + 
				"VALUES ( " + 
				":host," + 
				":port," + 
				":apptitle)";
			
			var sqlInsert:SQLStatement = new SQLStatement();
			sqlInsert.sqlConnection = _conn;
			//sqlInsert.addEventListener( SQLEvent.RESULT, onSQLSave );
			//sqlInsert.addEventListener( SQLErrorEvent.ERROR, onSQLError );				
			
			sqlInsert.text = query;
			sqlInsert.parameters[":host"] = config.host;
			sqlInsert.parameters[":port"] = config.port; 
			sqlInsert.parameters[":apptitle"] = config.apptitle; 
			
			
			sqlInsert.execute();	
		}
		
		
		public function update(config:Config):void {
			var query:String = "UPDATE config " + 
				"set " + 
				"host ='" + config.host +  "', " + 
				"port ='" + config.port +  "', " + 
				"apptitle ='" + config.apptitle +  "' " +
				"WHERE id = " + config.id + " ";
			
			var sqlInsert:SQLStatement = new SQLStatement();
			sqlInsert.sqlConnection = _conn;
			//sqlInsert.addEventListener( SQLEvent.RESULT, onSQLSave );
			//sqlInsert.addEventListener( SQLErrorEvent.ERROR, onSQLError );				
			
			sqlInsert.text = query;
			
			sqlInsert.execute();	
		}

		
	}
}